package gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;

import javax.swing.WindowConstants;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.SpinnerListModel;

import exceptions.MissingMandantoryFieldException;
import exceptions.UserAlreadyExistException;
import exceptions.WrongPasswordException;

import logic.Facade;
import logic.ToGuiFacade;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class pnlUserRegistration extends javax.swing.JPanel {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private JButton sumbitBtn;
	private JTextField userNameFld;
	private JPasswordField pass2Fld;
	private JLabel headerLbl;
	private static JLabel alertLabel;
	private JComboBox sexCmb;
	private JSpinner yearSpnr;
	private JSpinner monthSpnr;
	private JLabel bDate;
	private JSpinner daySpnr;
	private JLabel password2Lbl;
	private JLabel Password;
	private JPasswordField pass1Fld;
	private JLabel LastNameLbl;
	private JLabel nameLbl;
	private JButton cancelBtn;

	/**
	* Auto-generated main method to display this
	* JPanel inside a new JFrame.
	*/
	public static void main(String[] args) {
		JFrame frame = new JFrame();
		frame.getContentPane().add(new pnlUserRegistration());
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.pack();
		frame.setVisible(true);
	}

	public pnlUserRegistration() {
		super();
		initGUI();
	}

	private void initGUI() {
		try {
			this.setPreferredSize(new java.awt.Dimension(487, 340));
			this.setLayout(null);
			this.setBackground(new java.awt.Color(255,128,0));
			{
				sumbitBtn = new JButton();
				this.add(sumbitBtn);
				sumbitBtn.setText("Submit");
				sumbitBtn.setBounds(376, 298, 100, 23);
				sumbitBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						sumbitBtnActionPerformed(evt);
					}
				});
			}
			{
				cancelBtn = new JButton();
				this.add(cancelBtn);
				cancelBtn.setText("Cancel");
				cancelBtn.setBounds(251, 299, 99, 21);
				cancelBtn.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						ToGuiFacade.getInstance().back();
						//ToGuiFacade.getInstance().showLoginScreen();
					}
				});
			}
			{
				userNameFld = new JTextField();
				this.add(userNameFld);
				userNameFld.setBounds(131, 64, 164, 21);
			}
			{
				nameLbl = new JLabel();
				this.add(nameLbl);
				nameLbl.setText("Name:");
				nameLbl.setBounds(22, 63, 39, 14);
			}
			{
				LastNameLbl = new JLabel();
				this.add(LastNameLbl);
				LastNameLbl.setText("Sex:");
				LastNameLbl.setBounds(22, 96, 54, 14);
			}
			{
				pass1Fld = new JPasswordField();
				this.add(pass1Fld);
				pass1Fld.setBounds(131, 130, 164, 21);
			}
			{
				pass2Fld = new JPasswordField();
				this.add(pass2Fld);
				pass2Fld.setBounds(131, 163, 164, 21);
			}
			{
				Password = new JLabel();
				this.add(getPassword());
				Password.setBounds(22, 127, 79, 19);
				Password.setText("Password:");
			}
			{
				password2Lbl = new JLabel();
				this.add(password2Lbl);
				password2Lbl.setText("Retype Password:");
				password2Lbl.setBounds(22, 162, 135, 14);
			}
			{
				String[] str = new String[31];
				for (int i=0;i<=30; i++){
					str[i]=Integer.toString(i+1);
				}

				SpinnerListModel daySpnrModel =
					new SpinnerListModel(str);
				daySpnr = new JSpinner();
				this.add(getDaySpnr());
				daySpnr.setModel(daySpnrModel);
				daySpnr.setBounds(131, 192, 39, 21);
				daySpnr.getEditor().setPreferredSize(new java.awt.Dimension(20, 17));
			}
			{
				bDate = new JLabel();
				this.add(bDate);
				bDate.setText("Birth Date:");
				bDate.setBounds(22, 195, 91, 14);
			}

			String[] months = new String[12];
			for (int i=0;i<=11; i++){
				months[i]=Integer.toString(i+1);
			}

			{
				SpinnerListModel monthSpnrModel =
					new SpinnerListModel(months);
				monthSpnr = new JSpinner();
				this.add(monthSpnr);
				monthSpnr.setModel(monthSpnrModel);
				monthSpnr.setBounds(182, 192, 37, 21);
				monthSpnr.getEditor().setPreferredSize(new java.awt.Dimension(18, 17));
			}

			String[] years = new String[100];
			for (int i=0;i<=99; i++){
				years[i]=Integer.toString(i+1910);
			}
			{
				ComboBoxModel sexCmbModel =
					new DefaultComboBoxModel(
							new String[] { "Male", "Female" });
				sexCmb = new JComboBox();
				this.add(sexCmb);
				sexCmb.setModel(sexCmbModel);
				sexCmb.setBounds(131, 97, 164, 21);
			}
			{
				SpinnerListModel yearSpnrModel =
					new SpinnerListModel(years);
				yearSpnr = new JSpinner();
				this.add(yearSpnr);
				yearSpnr.setModel(yearSpnrModel);
				yearSpnr.setBounds(231, 192, 50, 21);
				yearSpnr.getEditor().setPreferredSize(new java.awt.Dimension(31, 17));
			}
			{
				headerLbl = new JLabel();
				this.add(headerLbl);
				headerLbl.setText("User Registration");
				headerLbl.setBounds(22, 20, 229, 22);
				headerLbl.setFont(new java.awt.Font("Arial",1,14));
			}
			{
				alertLabel = new JLabel();
				this.add(alertLabel);
				alertLabel.setBounds(22, 225, 453, 66);
				alertLabel.setForeground(new java.awt.Color(0,128,255));
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void setAlertLabel(String str){
		alertLabel.setText(str);
	}

	public JLabel getPassword() {
		return Password;
	}

	public JSpinner getDaySpnr() {
		return daySpnr;
	}

	private void sumbitBtnActionPerformed(ActionEvent evt) {
		//System.out.println("sumbitBtn.actionPerformed, event="+evt);
		try {
			String sex;
			if (this.sexCmb.getSelectedItem().toString().equals("Female"))
				sex="true";
			else sex="false";
			System.out.println("the selected sex at creation" + this.sexCmb.getSelectedItem().toString());
			Facade.getInstance().registerUser(this.userNameFld.getText(), String.copyValueOf(this.pass1Fld.getPassword()), new String(this.pass2Fld.getPassword()),daySpnr.getValue().toString(),monthSpnr.getValue().toString(),yearSpnr.getValue().toString() , sex);
		} catch (WrongPasswordException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MissingMandantoryFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UserAlreadyExistException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}



}
